--[[
 -- *名称:CreateRoomLogic
 -- *描述:创建房间逻辑
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:terry
 -- *创建日期:2017-11-27
 -- *修改日期:2017-11-27
 ]]
local CreateRoomLogic = class("CreateRoomLogic");
---
 -- 需要导出给绑定类使用的方法
 --
CreateRoomLogic.EXPORT_METHODS = {
	"updateCreateRoomNode",
	"getServerData",
};

--模板数据
local templateData = {
 ["template1"] = {itemHeight = 40,itembg = "",itemColor={255,255,255},labelSize = 24,labelColor={255,255,255},labelFontName="app/part/ddz/FZY3JW_1.TTF",subItemWidth=150,
	titleWidth = 100,checkboxwidth = 34,checkboxheight=36,divideHeight = 2,
	checkboxBgNormalImg = "app/part/ddz/components/lobby/createroom/res/img_selectbox.png",
	checkboxBgSelectImg = "app/part/ddz/components/lobby/createroom/res/img_selectbox.png",
	checkboxBgDisableImg = "app/part/ddz/components/lobby/createroom/res/img_selectbox.png",
	checkboxFrontSelectImg = "app/part/ddz/components/lobby/createroom/res/img_selectbox_checked.png",
	checkboxFrontDisableImgPath="app/part/ddz/components/lobby/createroom/res/img_selectbox_checked.png",
	checkboxPlist="app/part/ddz/components/lobby/createroom/res/createroom_plist.plist"};
}

local handler2 = function(self,data,node,fuc)
	return function(...)
		return fuc(self,data,node,...);
	end
end

--[[
 -- 创建方法
 -- @DateTime 2017-11-28
 -- @return   [description]
 ]]
function CreateRoomLogic:ctor(part)
	self._target = part ;
	self.templates = {} ;
	self.templates["template1"] = templateData["template1"] ; --加载模板一
end

function CreateRoomLogic:setPart(part)
	self._target = part ;
end

function CreateRoomLogic:addTemplate(name,template)
	self.templates[name] = template ;
end

function CreateRoomLogic:getPart()
	return self._target ;
end
--[[
 -- 添加回调
 -- @DateTime 2017-11-28
 ]]
function CreateRoomLogic:addAllCheckBoxClickListener(fuc)
	self.callBackFuc = fuc ;
end

function CreateRoomLogic:dtor()
end

--[[
 -- 点击当前节点
 -- @DateTime 2017-11-27
 -- @param    data 当前具体是点击的行列
 -- @param     [description]
 -- @return    [description]
 ]]
function CreateRoomLogic:clickCheckBox(data,root,...)
	local row,col = data.row,data.col ;
	--Toast.showText("col->"..col);
	local currentData  =  self.data[row] ;
    if currentData.itemType == "RadioGroup" then --其他所有选项都要取消
    	for i,v in ipairs(currentData.desc) do
    		local checkbox =root:getChildByName("sublayout"..i):getChildByName("checkbox"..i);
    		if i ~= col then
				 checkbox:setSelected(false);
				 checkbox:setTouchEnabled(true)
			else
				 checkbox:setTouchEnabled(false)
				 checkbox:setSelected(true);
    		end
    	end
    	 self.serverData[self.index][currentData.name] = currentData.value[col];
    elseif currentData.itemType == "CheckBox" then
    	 self.serverData[self.index][currentData.name] = not self.serverData[self.index][currentData.name];
    	 local checkbox =root:getChildByName("sublayout"..col):getChildByName("checkbox"..col);
    	 checkbox:setSelected(self.serverData[self.index][currentData.name]);
    end
    --接口回调
    if self.callBackFuc then 
   	     self.callBackFuc(self._target,data,self.listViewList[self.index],self.serverData[self.index],self.index);
   	end
end

--[[
 -- 获取传递server数据
 -- @DateTime 2017-11-27
 -- @return   [description]
 ]]
function CreateRoomLogic:getServerData()
	return self.serverData[self.index];
end

function CreateRoomLogic:loadCheckBoxTexture(checkbox,config)
	if config.checkboxPlist then
		--加载三张图片
		cc.SpriteFrameCache:getInstance():addSpriteFrames(config.checkboxPlist)
		checkbox:loadTextureBackGround(config.checkboxBgNormalImg,1)
		cc.SpriteFrameCache:getInstance():addSpriteFrames(config.checkboxPlist)
		checkbox:loadTextureBackGroundSelected(config.checkboxBgSelectImg,1)
		checkbox:loadTextureBackGroundDisabled(config.checkboxBgDisableImg,1)
		cc.SpriteFrameCache:getInstance():addSpriteFrames(config.checkboxPlist)
		checkbox:loadTextureFrontCross(config.checkboxFrontSelectImg,1)
		cc.SpriteFrameCache:getInstance():addSpriteFrames(config.checkboxPlist)
		checkbox:loadTextureFrontCrossDisabled(config.checkboxFrontDisableImgPath,1)
	else
		checkbox:loadTextureBackGround(config.checkboxBgNormalImg,0)
		checkbox:loadTextureBackGroundSelected(config.checkboxBgSelectImg,0)
		checkbox:loadTextureBackGroundDisabled(config.checkboxBgDisableImg,0)
		checkbox:loadTextureFrontCross(config.checkboxFrontSelectImg,0)
		checkbox:loadTextureFrontCrossDisabled(config.checkboxFrontDisableImgPath,0)
	end
end

--类型配置
function CreateRoomLogic:createRoomItem(row,itemData)
	local itemPanel = ccui.Layout:create()
	--itemPanel:setBackGroundColor({r = 150, g = 200, b = 255});
	--itemPanel:setBackGroundColorType(1)
	local itemConfig = self.templates[itemData.template] ;
	itemPanel:ignoreContentAdaptWithSize(false)
	itemPanel:setClippingEnabled(false)
	--itemPanel:setBackGroundImageCapInsets({x = -13, y = -13, width = 26, height = 26})
	itemPanel:setBackGroundColorOpacity(102)
	itemPanel:setBackGroundImageScale9Enabled(true)
	itemPanel:setTouchEnabled(true);
	itemPanel:setLayoutComponentEnabled(true)
	itemPanel:setName(itemData.name)
	itemPanel:setCascadeColorEnabled(true)
	itemPanel:setCascadeOpacityEnabled(true)
	itemPanel:setAnchorPoint(0, 0.5000)
	local layout = ccui.LayoutComponent:bindLayoutComponent(itemPanel)
	local totalHeight = itemConfig.itemHeight*itemData.lines  ;--总宽度,计算了行数
	if  itemData.lines >1 then
		layout:setSize({width = self.listViewSize.width, height = totalHeight})
    else
		layout:setSize({width = self.listViewSize.width, height = totalHeight+(itemData.lines-1)*itemConfig.divideHeight})
    end
    --Create title
	local title = ccui.Text:create()
	title:ignoreContentAdaptWithSize(true)
	title:setTextAreaSize({width = 0, height = 0})
	title:setFontName(itemConfig.labelFontName)
	title:setFontSize(itemConfig.labelSize)
	title:setString(itemData.title)       --[[局数选择：]]
	title:setLayoutComponentEnabled(true)
	title:setName("title")
	title:setCascadeColorEnabled(true)
	title:setCascadeOpacityEnabled(true)
	title:setAnchorPoint(0.0000, 0.5000)
	title:setPosition(0.0000,totalHeight - itemConfig.itemHeight/2)
	layout = ccui.LayoutComponent:bindLayoutComponent(title)
	layout:setSize({width = itemConfig.titleWidth, height = itemConfig.itemHeight})
	itemPanel:addChild(title)
	local cStartX = itemConfig.titleWidth ; --初始长度
	local perCount = 0; --计算每一行的个数
	if #itemData.desc > 0 then
		perCount =  #itemData.desc/itemData.lines
		if perCount > math.floor(perCount) then
			perCount =  math.floor(perCount) + 1 ;
		end
	end
	if #itemData.desc == 8 then
		--Toast.showText("perCount->"..perCount);
	end
	--创建单选框
	--Create checkbox
	for i,v in ipairs(itemData.desc) do

			--创建layout
			local subLayout = ccui.Layout:create() ;
			--subLayout:setBackGroundColor({r = 60, g = 200, b = 255});
			--subLayout:setBackGroundColorType(1)
			subLayout:ignoreContentAdaptWithSize(false)
			subLayout:setClippingEnabled(false)
			--itemPanel:setBackGroundImageCapInsets({x = -13, y = -13, width = 26, height = 26})
			subLayout:setBackGroundColorOpacity(102)
			subLayout:setBackGroundImageScale9Enabled(true)
			subLayout:setTouchEnabled(true);
			subLayout:setLayoutComponentEnabled(true)
			subLayout:setName("sublayout"..i);
			subLayout:setCascadeColorEnabled(true)
			subLayout:setCascadeOpacityEnabled(true)
			subLayout:setAnchorPoint(0.0000, 0.5000)
			subLayout:setClippingEnabled(true)
			if i <= perCount then
				subLayout:setPosition(cStartX + 20*i + (i-1)*itemConfig.subItemWidth,totalHeight- itemConfig.itemHeight/2)
			else --换行
				local currentCount = i/perCount;
				if currentCount > math.floor(currentCount) then
					currentCount = math.floor(currentCount) ;
				else
					currentCount = currentCount - 1 ;
				end
				subLayout:setPosition(cStartX + 20*(i-perCount*currentCount) + (i-perCount*currentCount-1)*itemConfig.subItemWidth,totalHeight- itemConfig.itemHeight/2 -itemConfig.itemHeight*(currentCount) -itemConfig.divideHeight*currentCount)
			end
		    layout = ccui.LayoutComponent:bindLayoutComponent(subLayout)
			layout:setSize({width = itemConfig.subItemWidth, height = itemConfig.itemHeight})

			local checkbox = ccui.CheckBox:create()
			checkbox:ignoreContentAdaptWithSize(false)
			self:loadCheckBoxTexture(checkbox,itemConfig);
			if itemData.itemType == "RadioGroup" then
				if itemData.defaultIndx == i then
				   checkbox:setSelected(true) ;
				   checkbox:setTouchEnabled(false)
				else
				   checkbox:setSelected(false) ;
				   checkbox:setTouchEnabled(true)
				end
			else
				checkbox:setSelected(itemData.defaultIndx == i) ;
			end
			checkbox:setLayoutComponentEnabled(true)
			checkbox:setName("checkbox"..tostring(i));
			checkbox:setCascadeColorEnabled(true)
			checkbox:setCascadeOpacityEnabled(true)
			checkbox:setAnchorPoint(0, 0.5000)
			checkbox:setPosition(0, itemConfig.itemHeight/2)
			checkbox:setContentSize({width = itemConfig.checkboxwidth, height = itemConfig.checkboxheight});
			checkbox:addEventListener(handler2(self,{row=row,col=i},itemPanel,self.clickCheckBox))
			layout = ccui.LayoutComponent:bindLayoutComponent(checkbox)
			layout:setSize({width = itemConfig.checkboxwidth, height = itemConfig.checkboxheight})
			subLayout:addChild(checkbox)
			--Create subtitle
			local subtitle = ccui.Text:create()
			subtitle:ignoreContentAdaptWithSize(true)
			subtitle:setTextAreaSize({width = 0, height = 0})
			subtitle:setFontName(itemConfig.labelFontName)
			subtitle:setFontSize(itemConfig.labelSize)
			subtitle:setString(v)
			subtitle:setLayoutComponentEnabled(true)
			subtitle:setName("subtitle")
			subtitle:setCascadeColorEnabled(true)
			subtitle:setCascadeOpacityEnabled(true)
			subtitle:setAnchorPoint(0.0000, 0.5000)
			subtitle:setPosition(50.0000,itemConfig.itemHeight/2)
			layout = ccui.LayoutComponent:bindLayoutComponent(subtitle)
			layout:setSize({width = itemConfig.subItemWidth-34, height = itemConfig.itemHeight})
			subLayout:addChild(subtitle)

			--Create btn_mask
			local btn_mask = ccui.Button:create()
			btn_mask:ignoreContentAdaptWithSize(false)
			btn_mask:loadTextureNormal("Default/Button_Normal.png",0)
			btn_mask:loadTexturePressed("Default/Button_Press.png",0)
			btn_mask:loadTextureDisabled("Default/Button_Disable.png",0)
			btn_mask:setTitleFontSize(14)
			btn_mask:setTitleColor({r = 65, g = 65, b = 70})
			btn_mask:setScale9Enabled(true)
			btn_mask:setCapInsets({x = 15, y = 11, width = 16, height = 14})
			btn_mask:setLayoutComponentEnabled(true)
			btn_mask:setName("btn_mask")
			btn_mask:setCascadeColorEnabled(true)
			btn_mask:setCascadeOpacityEnabled(true)
			btn_mask:setAnchorPoint(0.0000, 0.5000)
			btn_mask:setPosition(0.0000,itemConfig.itemHeight/2)
			btn_mask:setOpacity(0)
			-- if callBackProvider~=nil then
			btn_mask:addClickEventListener(handler2(self,{row=row,col=i},itemPanel,self.clickCheckBox))
			-- end
			layout = ccui.LayoutComponent:bindLayoutComponent(btn_mask)
			layout:setSize({width = itemConfig.subItemWidth, height = itemConfig.itemHeight})
			subLayout:addChild(btn_mask)
			itemPanel:addChild(subLayout)
	end
   return itemPanel ;
end

function CreateRoomLogic:getRoomNode(data)
	local itemList = {} ;
	for k,v in ipairs(data) do
		self.serverData[self.index][v.name] = v.value[v.defaultIndx] ;
		table.insert(itemList,self:createRoomItem(k,v));
	end
	return itemList ;
end

function CreateRoomLogic:createListView(node,index)
	local parentSize = node:getContentSize();
	local listView = ccui.ListView:create()
	listView:setItemsMargin(10)
	listView:setDirection(1)
	listView:setGravity(0)
	listView:ignoreContentAdaptWithSize(false)
	listView:setClippingEnabled(false)
	listView:setBackGroundColorOpacity(102)
	listView:setLayoutComponentEnabled(true)
	listView:setName("listView"..index)
	listView:setCascadeColorEnabled(true)
	listView:setCascadeOpacityEnabled(true)
	listView:setPosition(0, 0)
	--listView:setBackGroundColor({r = 60, g = 200, b = 255});
			--subLayout:setBackGroundColorType(1)
	local layout = ccui.LayoutComponent:bindLayoutComponent(listView)
	layout:setSize({width = parentSize.width, height = parentSize.height})
	
	node:addChild(listView)
	return listView ;
end

--[[
 -- 更新节点
 -- @DateTime 2017-11-28
 -- @param    index [description]
 -- @param    node [description]
 -- @param    data [description]
 -- @return    [description]
 ]]
function CreateRoomLogic:updateCreateRoomNode(index,node,data)
    self.data = data ; --存储数据
    self.index = index ;
    self.serverData = self.serverData or {} ;
    self.serverData[index] = self.serverData[index] or {} ; --数据缓存
    self.listViewList = self.listViewList or {} ; --listView节点
    if not self.listViewList[index]  then
   		 local listView =  self:createListView(node,index);
   		 self.listViewSize = listView:getContentSize();

		 local itemList = self:getRoomNode(data,listView);
		 for k,v in ipairs(itemList) do
		    listView:pushBackCustomItem(v) ;
		 end
		 self.listViewList[index] = listView ;
	end
	
	for k,v in pairs(self.listViewList) do
		if k ~= index then
			self.listViewList[k]:hide();
		else
			self.listViewList[k]:show();
		end
	end
	--一开始回调,通知part更新
	if self.callBackFuc then 
   	     self.callBackFuc(self._target,nil,self.listViewList[index],self.serverData[self.index]);
   	end
end

return CreateRoomLogic;